//
//  FHStatusOriginalView.swift
//  FHweibo
//
//  Created by 张嵩雷 on 16/4/14.
//  Copyright © 2016年 feng. All rights reserved.
//

import UIKit

///微博原创view
class FHStatusOriginalView: UIView {
    
    //微博模型
    var status: FHStatus? {
        didSet {
            // cell将模型设置给微博原创view后,微博原创view自己来设置自己的内容
            //设置头像
            if let profile_image_url = status?.user?.profile_image_url{
                //有图片地址
                let url = NSURL(string: profile_image_url)!
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            
            //用户名称
            nameLabel.text = status?.user?.screen_name
            //时间
            timeLabel.text = status?.created_at
            //来源
            sourceLabel.text = status?.source
            //微博内容
            contentLabel.text = status?.text
            
            //设置用户的会员等级
//            if let mbrank = status?.user?.mbrank {
//                if mbrank > 0 && mbrank <= 6 {
//                    //设置会员等级
//                    memberView.image = UIImage(named: "common_icon_membership_level\(mbrank)")
//                }else {
//                    //防止cell重用
//                    memberView.image = nil
//                }
//            }
            memberView.image = status?.user?.mbrankImage
            //认证类型
            verifiedView.image = status?.user?.verifiedImage
            
//            if let verified_type = status?.user?.verified_type {
//                 // 没有认证:-1   认证用户:0  企业认证:2,3,5  达人:220
//                switch verified_type {
//                case 0: verifiedView.image = UIImage(named: "avatar_vip")
//                case 2,3,5: verifiedView.image = UIImage(named: "avatar_enterprise_vip")
//                case 220: verifiedView.image = UIImage(named: "avatar_grassroot")
//                default: verifiedView.image = nil
//                }
//                
//            }
            
        }
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.whiteColor()
        
        prepareUI()
    }

    private func prepareUI() {
        //1.添加子控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(memberView)
        addSubview(verifiedView)
        addSubview(contentLabel)
        
        //2.添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(FHStatusCellMargin)
            make.size.equalTo(CGSize(width: FHStatusIconWH, height: FHStatusIconWH))
        }
        //名称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(FHStatusCellMargin)
        }
        //时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(iconView)
            make.leading.equalTo(nameLabel)
        }
        //来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(FHStatusCellMargin)
            make.centerY.equalTo(timeLabel)
        }
        //会员等级
        memberView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(FHStatusCellMargin)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        //认证图标
        verifiedView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSize(width: 17, height: 17))
        }
        
        //微博内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(FHStatusCellMargin)
            make.top.equalTo(iconView.snp_bottom).offset(FHStatusCellMargin)
            make.trailing.equalTo(self).offset(-FHStatusCellMargin)
        }
        
        ///自定义的原创微博view的底部和contentlabel的底部重合,偏移8
        self.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(contentLabel).offset(FHStatusCellMargin)
        }
        
    }
    
    
    //MARK: - 懒加载
    ///头像
    private lazy var iconView: UIImageView = {
       let imageView = UIImageView(image: UIImage(named: "avatar"))
        
        //设置
        imageView.layer.cornerRadius = 17.5
        imageView.layer.masksToBounds = true
        
        return imageView
        
    }()
    
    ///用户名称label
    private lazy var nameLabel: UILabel = UILabel(fontSize: FHStatusNameFontSize, textColor: UIColor.darkGrayColor())
    
//    private lazy var nameLabel: UILabel = {
//       let label = UILabel()
//        
//        //设置
//        label.font = UIFont.systemFontOfSize(FHStatusNameFontSize)
//        label.textColor = UIColor.darkGrayColor()
//        
//        return label
//        
//    }()
    
    //时间label
    private lazy var timeLabel: UILabel = UILabel(fontSize: FHStatusTimeFontSize, textColor: UIColor.orangeColor())
    
//    private lazy var timeLabel: UILabel = {
//        let label = UILabel()
//        
//        //设置
//        label.font = UIFont.systemFontOfSize(FHStatusTimeFontSize)
//        label.textColor = UIColor.orangeColor()
//        
//        return label
//    }()
    
    //来源label
    private lazy var sourceLabel: UILabel = UILabel(fontSize: FHStatusTimeFontSize, textColor: UIColor.lightGrayColor())
    
//    private lazy var sourceLabel: UILabel = {
//        let label = UILabel()
//        
//        //设置
//        label.font = UIFont.systemFontOfSize(FHStatusTimeFontSize)
//        label.textColor = UIColor.lightGrayColor()
//        
//        return label
//    }()
    
    ///会员图标
    private lazy var  memberView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
    
    ///认证图标
    private lazy var  verifiedView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    ///原创微博内容
    private lazy var contentLabel: UILabel = {
       let label = UILabel()
        
        //设置
        label.font = UIFont.systemFontOfSize(FHStatusContentFontSize)
        label.textColor = UIColor.darkGrayColor()
        
        let result = arc4random_uniform(100) % 2
        label.text = result == 0 ? "go die" : "大山地阿迪爱打算打算的奥是单身到静安寺多看书大叔大婶单身的爱上的那是打是打算打算的打算打算的爱谁谁的速度"
        label.sizeToFit()
        label.numberOfLines = 0
        
        return label
    }()
    
    
}
